<?php

namespace p5\objects;

/**
 * Created by PhpStorm.
 * User: simon
 * Date: 2015/11/23
 * Time: 9:35
 */
class HtmlUtils
{
    public static function tag($name, $attributes = array(), $innerHTML = "")
    {
        $html = "";
        $html .= "<" . $name;
        while (list($key, $value) = each($attributes)) {
            if ("checked" == $key) {
                if ($value == true) {
                    $html .= " checked";
                }
            } else {
                $html .= " " . $key . "=\"" . static::value_replace($value) . "\"";
            }
        }
        if (!empty($innerHTML)) {
            $html .= ">";
            $html .= $innerHTML;
            $html .= "</$name>";
        } else {
            $html .= "/>";
        }
        return $html;
    }

    static public function value_replace($str)
    {
        $str = str_replace("<", "&lt;", $str);
        $str = str_replace(">", "&gt;", $str);
        $str = str_replace("\"", "&quot;", $str);
        return $str;
    }


    private static $elementIdIndex = 0;
    private static $elementIdSet = array();

    public static function next_id($defaultValue)
    {
        $id = $defaultValue;
        if (in_array($defaultValue, self::$elementIdSet)) {
            $id = $defaultValue . "_" . self::$elementIdIndex;
            self::$elementIdIndex++;
        }
        array_push($elementIdSet, $id);
        return $id;
    }

    public static function to_html($str)
    {
        $string = $str;
        $string = str_replace("<", "&lt;", $string);
        $string = str_replace(">", "&bt;", $string);
        $string = str_replace("\n", "<br>", $string);
        $string = str_replace(" ", "&nbsp;", $string);
        return $string;
    }

    public static function make_select($name, $matrix, $default_value = "", $nullable = 1, $emptyLabel = "", $attributes = array())
    {
        if (!isset($emptyLabel)) {
            $emptyLabel = "";
        }
        if (empty($defaultValues)) {
            $defaultValues = SessionUtils::deep_get($name);
        }
        $attributes["name"] = $name;
        $attributes["id"] = $name;
        $html = self::make_options($matrix, $default_value, $nullable, $emptyLabel);
        return self::tag("select", $attributes, $html);
    }

    public static function make_options($matrix, $default_value = "", $nullable = 1, $emptyLabel = "")
    {
        $html = "";
        if ($nullable) {
            $html .= "<option value=''>$emptyLabel</option>";
        }
        while (list($key, $value) = each($matrix)) {
            $selected = "";
            if (isset($default_value) && $key . "kk" == $default_value . "kk") {
                $selected = " selected";
            }
            $txt = $value;
            $html .= "<option value=\"" . $key . "\"$selected>" . $txt . "</option>";
        }
        unset($matrix);
        return $html;
    }

    public static function make_checkbox($name, $matrix, $list = array(), $attributes = array(), $cols = 0)
    {
        if (empty($matrix)) {
            return "";
        }

        $string = "";

        $w = 100;
        if ($cols > 0) {
            $string .= "<div class='div_boxies'>";
            $string .= "<table class='table_box'>";
            $w = round(100 / $cols);
        }

        $attributes["name"] = $name;
        $attributes["id"] = $name;
        $attributes["type"] = "checkbox";

        $i = 0;
        while (list($key, $value) = each($matrix)) {
            if ($cols > 0 && $i > 0 && $i % $cols == 0) {
                if ($i != 0) {
                    $string .= "</tr>";
                }
                $string .= "<tr>";
            }

            if ($name == "_KEY_") {
                $name = $key;
            }
            $simple_name = str_replace("[]", "", $name);

            if ($cols > 0) {
                $string .= "<td style='width:" . $w . "%;' id='td_" . $simple_name . "_$key'>";
            }

            $attributes["value"] = $key;
            $attributes["id"] = $simple_name . "_" . $key;
            if (is_array($list) && in_array($key, $list)) {
                $attributes["checked"] = true;
            } else {
                $attributes["checked"] = false;
            }

            $string .= tag("input", $attributes);
            $string .= $value;

            if ($cols > 0) {
                $string .= "</td>";
            }
            $i++;
        }

        if ($cols > 0) {
            $mod = $i % $cols;
            if ($i % $cols != 0) {
                for ($t = 0; $t < ($cols - $mod); $t++) {
                    $string .= "<td></td>";
                }
                $string .= "</tr>";
            }
            $string .= "</table></div>";
        }

        return $string;
    }

    public static function make_radio($name, $matrix, $defaultValue = "", $attributes = array(), $cols = 0)
    {
        if (empty($defaultValues)) {
            $defaultValues = SessionUtils::deep_get($name);
        }
        if (count($matrix) == 0) {
            return "";
        }
        $string = "";
        $w = 100;

        if ($cols > 0) {
            $string .= "<div class='div_boxies'>";
            $string .= "<table class='table_box'>";
            $w = round(100 / $cols);
        }

        $attributes["name"] = $name;
        $attributes["id"] = $name;
        $attributes["type"] = "radio";

        $i = 0;
        while (list($key, $value) = each($matrix)) {
            if ($cols > 0 && $i > 0 && $i % $cols == 0) {
                if ($i != 0) {
                    $string .= "</tr>";
                }
                $string .= "<tr>";
            }
            if ($cols > 0) {
                $string .= "<td style='width:" . $w . "%;'>";
            }

            $attributes["value"] = $key;
            $attributes["id"] = $name . "_" . $key;
            if ("a" . $key == "a" . $defaultValue || "a" . $value == "a" . $defaultValue) {
                $attributes["checked"] = true;
            } else {
                $attributes["checked"] = false;
            }
            $string .= tag("input", $attributes);

            $string .= $value;

            if ($cols > 0) {
                $string .= "</td>";
            }
            $i++;
        }

        if ($cols > 0) {
            $mod = $i % $cols;
            if ($i % $cols != 0) {
                for ($t = 0; $t < ($cols - $mod); $t++) {
                    $string .= "<td></td>";
                }
                $string .= "</tr>";
            }
            $string .= "</table></div>";
        }
        return $string;
    }


    public static function make_boolean_radio($name, $defaultValue = 0, $attributes = array())
    {
        $matrix = array(
            "1" => "是"
        , "0" => "否"
        );
        return self::make_radio($name, $matrix, $defaultValue, $attributes);
    }

    public static function make_gender_radio($name = "gender", $defaultValue = "0", $attributes = array())
    {
        $matrix = array(
            "0" => "未知"
        , "1" => "男"
        , "2" => "女"
        );
        return self::make_radio($name, $matrix, $defaultValue, $attributes);
    }

    public static function get_date_options()
    {
        $matrix = array();
        $matrix["TODAY"] = "今天";
        $matrix["YESTERDAY"] = "昨天";
        $matrix["THIS_WEEK"] = "本周";
        $matrix["LAST_WEEK"] = "上周";
        $matrix["LAST_7_DAYS"] = "近7天内";
        $matrix["THIS_MONTH"] = "本月";
        $matrix["LAST_MONTH"] = "上个月";
        $matrix["LAST_30_DAYS"] = "近30天内";
        $matrix["THIS_YEAR"] = "今年";
        $matrix["LAST_YEAR"] = "去年";
        // $matrix[""] = "自定义";
        return $matrix;
    }


    public static function get_brief($txt, $length, $start = 0)
    {
        $str = $txt;
        if (strlen($str) < $start + 1) {
            return '';
        }
        preg_match_all("/./su", $str, $ar);
        $str = '';
        $tstr = '';

        for ($i = 0; isset($ar[0][$i]); $i++) {
            if (strlen($tstr) < $start) {
                $tstr .= $ar[0][$i];
            } else {
                if (strlen($str) < $length + strlen($ar[0][$i])) {
                    $str .= $ar[0][$i];
                } else {
                    break;
                }
            }
        }
        if ($str != $txt) {
            $str = $str . "...";
        }

        return $str;
    }

    public static function shorten($txt, $len = 10, $addTitle = true)
    {
        $result = self::get_brief($txt, $len);
        if (($result != $txt) && $addTitle) {
            return "<span title=\"" . self::escape_quot($txt) . "\">$result</span>";
        }
        return $result;
    }

    public static function escape_quot($string)
    {
        if (empty($string)) {
            return "";
        }
        $txt = str_replace('"', "&quot;", $string);
        return $txt;
    }

    public static function html_escape($txt)
    {
        if (empty($txt)) {
            return "";
        }
        $txt = str_replace(' ', "&nbsp;", $txt);
        $txt = str_replace('<', "&lt;", $txt);
        $txt = str_replace('>', "&gt;", $txt);
        $txt = str_replace('&', "&amp;", $txt);
//        $txt = str_replace('©', "&copy;", $txt);
//        $txt = str_replace('®', "&reg;", $txt);
        $txt = str_replace('"', "&quot;", $txt);
        return $txt;
    }
}